//
//  HomeViewController.swift
//  1010
//
//  Created by 米明 on 15/7/23.
//  Copyright (c) 2015年 米明. All rights reserved.
//

import UIKit
import Social
import MessageUI

//闭包
typealias CallBack = () -> Void

//MARK:自定义颜色
func customColor(red:CGFloat ,green:CGFloat, blue:CGFloat, alpha: CGFloat) -> UIColor {
    return UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha*0.89)
}

class HomeViewController: UIViewController,MFMailComposeViewControllerDelegate {

    var menu : MenuView!
    var emallController : MFMailComposeViewController!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let fonts = UIFont.familyNames
        print("\(fonts)")
        // Do any additional setup after loading the view.
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.view.backgroundColor=LYColor.BackColor()
        if menu == nil {
            homeMenu()
            menu.showHomeMenu()
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
    }
    
    func homeMenu() {
        menu = MenuView(frame: UIScreen.main.bounds)
        
        //计分板
        menu.scoreCallBack(callBack: {
            LYProgressHUD.showMessage(message: "暂无排名!")
        })
        
        //设置
        menu.settingCallBack(callBack: {
            var setting:LYSettingView!
            setting = LYSettingView(frame: CGRect.zero)
            setting.infoActionCallBack(callBack: { (index) -> () in
                switch index {
                case 1:
                    self.view.backgroundColor=LYColor.BackColor()
                case 2:
                    let info=InfoViewController()
                    self.navigationController?.pushViewController(info, animated: false)
                    
                case 3:
                    if MFMailComposeViewController.canSendMail() {
                        self.emallController = MFMailComposeViewController()
                        self.emallController.mailComposeDelegate=self
                        self.emallController.setToRecipients(["ly-918@163.com"])
                        self.emallController.setSubject("意见反馈")
                        self.emallController.setMessageBody("请填写您的意见和建议!非常感谢!", isHTML: true)
                        self.present(self.emallController, animated: true, completion: {
                            
                        })
                    }else{
                        LYProgressHUD.showMessage(message: "不支持Email")
                    }
                default:break
                }
            })
            self.view.addSubview(setting)
        })
        
        //开始
        menu.startCallBack(callBack: {
            self.start()
        })
        
        //分享
        menu.shareCallBack(callBack: {
            var shareView:LYShareView!
            shareView = LYShareView(frame: CGRect.zero)
            shareView.shareActionCallBack(btnCallBack: { (index) -> () in
                let share = Share()
                var type:String!
                switch index {
                case 0: type = nil
                case 1: type = SLServiceTypeSinaWeibo
                case 2: type = SLServiceTypeTencentWeibo
                default :break
                }
                share.shareTo(type: type, image: UIImage(named: "Icon"), text: "这个游戏好好玩啊!", vc: self)
            })
            self.view.addSubview(shareView)
        })
        
        self.view.addSubview(menu)
    }
    
    func start() {
    
        if menu != nil {
            menu.removeFromSuperview()
            menu = nil
        }
        
        let game = GameViewController()
        game.modalPresentationStyle = .fullScreen
        self.present(game, animated: false, completion: nil)
    }
    
    //MARK: E-mall
    private func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
        print(result)
        self .dismiss(animated: true, completion: {
            switch result.rawValue {
            case 2:
                LYProgressHUD.showMessage(message: "感谢您的反馈!")
            case 3:
                LYProgressHUD.showMessage(message: "发送失败,请检查邮箱设置")
            default:break
            }
        })
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
